package cn.darkhorse.mesadmin.controller.device;

import cn.darkhorse.device.pojo.MesDeviceConsumable;
import cn.darkhorse.device.pojo.MesDeviceConsumableType;
import cn.darkhorse.device.service.MesDeviceConsumableService;
import cn.darkhorse.device.service.MesDeviceConsumableTypeService;
import cn.darkhorse.device.service.MesDeviceService;
import cn.darkhorse.device.service.MesDeviceConsumableTypeService;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

import static cn.darkhorse.common.constant.CommonConstant.ORDER_TYPE_DESC;

@Api(tags = "耗材类型管理")
@RestController
@RequestMapping("/device/mesDeviceConsumableType")
public class MesDeviceConsumableTypeController extends BaseController {
    @Resource
    private MesDeviceConsumableTypeService mesDeviceConsumableTypeService;
    @Resource
    private MesDeviceConsumableService mesDeviceConsumableService;


    /**
     * 获取耗材类型编码
     * @return
     */
    @ApiOperation(value = "获取耗材类型编码")
    @RequestMapping("/getDeviceConsumableTypeCode")
    public AjaxResult getDeviceConsumableTypeCode() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",System.currentTimeMillis());
        return AjaxResult.success(jsonObject);
    }

    /**
     * 添加或修改耗材类型
     * @param mesDeviceConsumableType
     * @return
     */
    @ApiOperation(value = "添加或修改耗材类型")
    @PostMapping("/save")
    public AjaxResult save(@Validated @RequestBody MesDeviceConsumableType mesDeviceConsumableType) {
        // Step.1 校验同公司下类型名称不可重复
        LambdaQueryWrapper<MesDeviceConsumableType> qw4DeviceConsumableType = new LambdaQueryWrapper<>();
        qw4DeviceConsumableType.eq(MesDeviceConsumableType::getTypeName, mesDeviceConsumableType.getTypeName().trim());
        if (mesDeviceConsumableType.getId() != null) {
            qw4DeviceConsumableType.ne(MesDeviceConsumableType::getId, mesDeviceConsumableType.getId());
        }
        List<MesDeviceConsumableType> types = mesDeviceConsumableTypeService.list(qw4DeviceConsumableType);
        if (null != types && !types.isEmpty()) {
            throw new ServiceException("耗材分类名称已存在");
        }

        // Step.2 保存
        if (null == mesDeviceConsumableType.getCreator()) {
            mesDeviceConsumableType.setCreator(SecurityUtils.getUserId());
        }

        // 修改需要更新修改时间
        if(null != mesDeviceConsumableType.getId()) {
          mesDeviceConsumableType.setGmtModify(LocalDateTime.now());
        }

        MesDeviceConsumableType entity = new MesDeviceConsumableType();
        BeanUtils.copyProperties(mesDeviceConsumableType, entity);

        boolean result = mesDeviceConsumableTypeService.saveOrUpdate(entity);
        return AjaxResult.success(result);
    }

    /**
     * 根据耗材类型Id获取耗材类型信息
     * @param id 耗材类型id
     * @return
     */
    @ApiOperation(value = "根据耗材类型Id获取耗材类型信息")
    @RequestMapping("/getMesDeviceConsumableType")
    public AjaxResult getMesDeviceConsumableType(Long id) {
        MesDeviceConsumableType DeviceConsumableType = mesDeviceConsumableTypeService.getById(id);
        return AjaxResult.success(DeviceConsumableType);
    }

    /**
     * 删除耗材类型
     * @param id
     * @return
     */
    @ApiOperation(value = "删除耗材类型")
    @RequestMapping("/del")
    public AjaxResult delMesDeviceConsumableType(Long id) {
        // Step.1 验证该类型下是否有设备，如有设备不允许删除
        LambdaQueryWrapper<MesDeviceConsumable> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MesDeviceConsumable::getType, id);
        List<MesDeviceConsumable> consumable = mesDeviceConsumableService.list(queryWrapper);
        if (consumable!= null && !consumable.isEmpty()) {
            throw new ServiceException("耗材类型已经使用，不允许删除");
        }

        // Step.2 删除
        boolean result = mesDeviceConsumableTypeService.removeById(id);
        return AjaxResult.success(result);
    }

    /**
     * 根据类型名称模糊查询耗材类型列表信息
     * @param typeName 类型名称
     * @param orderType 排序 asc desc
     * @return
     */
    @ApiOperation(value = "根据类型名称模糊查询耗材类型列表信息")
    @RequestMapping("/list")
    public TableDataInfo listMesDeviceConsumableType(String typeName, String orderType) {
        startPage();
        LambdaQueryWrapper<MesDeviceConsumableType> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(typeName)) {
            queryWrapper.like(MesDeviceConsumableType::getTypeName, typeName);
        }

        if (ORDER_TYPE_DESC.equals(orderType)) {
            queryWrapper.orderByDesc(MesDeviceConsumableType::getGmtCreate);
        }

        queryWrapper.orderByAsc(MesDeviceConsumableType::getGmtCreate);
        List<MesDeviceConsumableType> types = mesDeviceConsumableTypeService.list(queryWrapper);
        return getDataTable(types);
    }
}


